//
//  UserModel.m
//  VideoExam
//
//  Created by apple on 2020/4/21.
//

#import "UserModel.h"


#define user_data_key @"user_data_key"
@implementation UserModel
#pragma mark - < NSCoding >
- (void)encodeWithCoder:(NSCoder *)aCoder {
    [self yy_modelEncodeWithCoder:aCoder];
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    return [self yy_modelInitWithCoder:aDecoder];
}
+ (void)save_userModel:(UserModel *)model {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:model];
     [userDefaults setObject:data forKey:user_data_key];
     [userDefaults synchronize];//  同步数据
//   dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
//
//
//    });
}

/// 退出登录接口
/// @param success 成功
/// @param errorBlock errorBlock
+ (void)exit_Login_Success:(ObjSuccess)success error:(ErrorBlock)errorBlock {
    NSString *token = [UserModel get_userModel].token;
    NSMutableDictionary *dic = [NSMutableDictionary new];
    if ([token isHelpful]) {
        [dic setObject:token forKey:@"token"];
    }
    NSString * url = LogOut_Url;
    [NetWorkTools requestWithURLStr:url method:GET params:nil success:^(id responseObject) {
        NSLog(@"退出登录接口 =%@",responseObject);
          if ([AppTools isStateSuccess:responseObject]) {
                 if (success) {
                     success(nil);
                 }
             } else {
                 if (errorBlock) {
                     errorBlock();
                 }
             }
    } failure:^(NSError *error) {
      if (errorBlock) {
           errorBlock();
       }
       NSString *errorStr = [self errorDescribe:error];
       [QMUITips showError:errorStr];
    }];
    
}
+(UserModel *)get_userModel {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSData *data =   [userDefaults objectForKey:user_data_key];
    UserModel *model = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    return model;
}
+(void)delete_userModel {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults removeObjectForKey:user_data_key];
    [userDefaults synchronize];//  同步数据
}
@end
